package com.lanou.fooddemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.bean.PicBean;
import com.lanou.fooddemo.bean.SubCardsBean;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.util.CommonUtil;
import com.lanou.fooddemo.view.GalleryImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 2015/7/29.
 */
public class SameCityListAdapter extends BaseAdapter{
    private static final String LOG_TAG = "SameCityListAdapter";
    private LayoutInflater inflater;
    private List<DataBean> dataBeans;
    private List<SubCardsBean> subCardsBeans;

    public SameCityListAdapter(Context c, List<DataBean> beans) {
        inflater = LayoutInflater.from(c);
        dataBeans = beans;
    }

    @Override
    public int getCount() {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        subCardsBeans = new ArrayList<SubCardsBean>();
        Holder holder = null;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.view_same_city_data_list_item, null);
            holder = new Holder();
            holder.dataListItemUserIcImageView =
                    (SmartImageView) convertView.findViewById(R.id.dataListItemUserIcImageView);
            holder.dataListItemUserName =
                    (TextView) convertView.findViewById(R.id.dataListItemUserName);
            holder.dataListItemUserSign =
                    (TextView) convertView.findViewById(R.id.dataListItemUserSign);
            holder.dataListItemFoodImageView =
                    (GalleryImageView) convertView.findViewById(R.id.dataListItemFoodImageView);
            holder.dataListItemPlaceTextView =
                    (TextView) convertView.findViewById(R.id.dataListItemPlaceTextView);
            holder.dataListItemContent = (TextView) convertView.findViewById(R.id.dataListItemContent);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if (CommonUtil.isListNotNull(dataBeans)) {
            Log.i("liuyann", "adapter databeans size = " + dataBeans.size());
            DataBean dataBean = dataBeans.get(position);
            if (null != dataBean.getSubCards() &&
                    DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {
                subCardsBeans = dataBean.getSubCards();
            }
        }
        if (CommonUtil.isListNotNull(subCardsBeans)) {
            SubCardsBean subCardsBean = null;
            SubCardsBean subCardsBeanMap = null;
            SubCardsBean subCardsBeanImage = null;
            SubCardsBean subCardsBeanContent = null;
            for (SubCardsBean subCardsBeanTemp : subCardsBeans) {
                if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBean = subCardsBeanTemp;
                }
                if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType()) ||
                        SubCardsBean.TYPE_SLIDE_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanImage = subCardsBeanTemp;
                }
                if (SubCardsBean.TYPE_MAP_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanMap = subCardsBeanTemp;
                }
                if (SubCardsBean.TYPE_CONTENT_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanContent = subCardsBeanTemp;
                }
            }
            holder.dataListItemUserIcImageView.setImageUrl(subCardsBean.getUser().getAvatar(),
                    SmartImageView.TYPE_ROUND);
            holder.dataListItemUserName.setText(subCardsBean.getTitle());
            holder.dataListItemUserSign.setText(subCardsBean.getDesc());

            if (null == subCardsBeanImage.getPics()) {
                String imageUrl = subCardsBeanImage.getPic().getUrl();
                holder.dataListItemFoodImageView.addOnlyOneOfURLData(imageUrl);
            } else {
                List<String> imageUrl = new ArrayList<String>();
                List<PicBean> pics = subCardsBeanImage.getPics();
                for (PicBean pic : pics) {
                    imageUrl.add(pic.getUrl());
                    Log.i("liuyann", "pic url = " + pic.getUrl());
                }
                holder.dataListItemFoodImageView.addAllOfURLData(imageUrl);
            }

            holder.dataListItemPlaceTextView.setText(subCardsBeanMap.getTitle());
            holder.dataListItemContent.setText(subCardsBeanContent.getContent());
        }
        return convertView;
    }
    class Holder {
        SmartImageView dataListItemUserIcImageView;
        TextView dataListItemUserName;
        TextView dataListItemUserSign;
        GalleryImageView dataListItemFoodImageView;
        TextView dataListItemPlaceTextView;
        TextView dataListItemContent;
    }
}
